/******************************************************************
 *
 *    山东中软科
 *
 *    Copyright (c) 2019-2021
 *
 *    Package:     io.finer.erp.jeecg.bas.controller
 *
 *    Filename:    ZZShoporderCutController.java
 *
 *    Description: 
 *
 *    Copyright:   Copyright (c) 2019-2021
 *
 *    Company:     山东中软科
 *
 *    @author:     duany
 *
 *    @version:    1.0.0
 *
 *    Create at:   2020-12-22 08:54:25
 *
 *    Revision:
 *
 *    2020-12-22 08:54:25 - first revision by duany
 *
 *    Function List:
 *
 *****************************************************************/

package io.finer.erp.jeecg.bas.controller;

import javax.servlet.http.HttpServletRequest;

import io.finer.erp.jeecg.bas.entity.ZrkQzOrder;
import io.finer.erp.jeecg.bas.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.MaterialConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.finer.erp.jeecg.bas.entity.ZzShoporder;
import io.finer.erp.jeecg.bas.service.IZrkQzOrderService;
import io.finer.erp.jeecg.bas.service.IZzShoporderService;
import io.swagger.annotations.ApiOperation;

import java.util.*;

/**
 * @ClassName ZZShoporderCutController
 * @Description 分切工单controller
 * @author duany
 * @Date 2020-12-22 08:54:25
 * @version 1.0.0
 */
@RestController
@RequestMapping("/zrk/orderPart")
public class ZZShoporderPartController {
	@Autowired
	private IZzShoporderService zzShoporderService;
	@Autowired
	private IZrkQzOrderService orderService;
	
	/**
	 * 分页列表查询
	 * @param zzShoporder
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "车间任务/订单-分页列表查询")
	@ApiOperation(value="车间任务/订单-分页列表查询", notes="车间任务/订单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ZzShoporder zzShoporder,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ZzShoporder> queryWrapper = QueryGenerator.initQueryWrapper(zzShoporder, req.getParameterMap());
		queryWrapper.eq("process_id", "1300701063685087234");
		Page<ZzShoporder> page = new Page<ZzShoporder>(pageNo, pageSize);
		IPage<ZzShoporder> pageList = zzShoporderService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
    /**
     * @Description 复合切张工单生成复合切张mes单
     * @param createMes
     * @return
     * Create at: 2020-12-23 10:23:56
     * @author: duany
     * Revision:
     *    2020-12-23 10:23:56 - first revision by duany
     *        
     */
    @PostMapping("/createMesFHQZ")
    public Result<?> createMesFHQZ(@RequestBody CreateMesVO createMes){
		List<PickUpVo> pickUpVos=createMes.getList();
		String orderId = createMes.getOrderId();
		ZzShoporder shoporder=zzShoporderService.getById(orderId);
		if(shoporder == null){
			return Result.error("未找到任务单！");
		}
		//复合与制胶的不对比
		if(shoporder !=null && !shoporder.getProcessId().equals(MaterialConstant.PROCESS_COMPOUND)&&
				!shoporder.getProcessId().equals(MaterialConstant.PROCESS_ZJ)) {
			if (pickUpVos != null && pickUpVos.size() > 0) {
				for (PickUpVo pickUpVo : pickUpVos) {
					String categoryId = pickUpVo.getCategoryId();
					String taskcategoryId = shoporder.getCategoryId();
					if (!categoryId.equals(taskcategoryId)) {
						return Result.error("物料分类与任务单不符！");
					}
				}
			}
		}
    	return this.orderService.createMesFHQZ(createMes);
    }
    
    /**
     * @Description 分切工单生成分切mes单
     * @param createMes
     * @return
     * Create at: 2020-12-24 16:15:00
     * @author: duany
     * Revision:
     *    2020-12-24 16:15:00 - first revision by duany
     *        
     */
    @PostMapping("/createMesFQ")
    public Result<?> createMesFQ(@RequestBody CreateMesVO createMes){
    	List<PickUpVo> pickUpVos=createMes.getList();
    	String orderId = createMes.getOrderId();
//    	ZzShoporder shoporder=zzShoporderService.getById(orderId);
		String[] orderIdArray = StringUtils.split(orderId, ",");
		List<String> orderIdList = Arrays.asList(orderIdArray);
		List<ZzShoporder> byOrderIds = zzShoporderService.listByIds(orderIdList);
		if(byOrderIds == null || byOrderIds.size()==0){
			return Result.error("未找到任务单！");
		}
    	if(pickUpVos!=null && pickUpVos.size()>0){
			for (PickUpVo pickUpVo : pickUpVos) {
				String categoryId=pickUpVo.getCategoryId();
				String taskcategoryId=byOrderIds.get(0).getCategoryId();
				if(!categoryId.equals(taskcategoryId)){
					return Result.error("物料分类与任务单不符！");
				}
			}
		}
    	return this.orderService.createMesFQ(createMes);
    }
    
    @GetMapping("/getCustomer")
    public Result<?> getCustomer(String orderId){
		return this.zzShoporderService.getCustomer(orderId);
    }
    
    /**
     * @Description 创建分切MES单
     * @param createPart
     * @return
     * Create at: 2021-01-22 16:46:03
     * @author: duany
     * Revision:
     *    2021-01-22 16:46:03 - first revision by duany
     *        
     */
    @PostMapping("/createPart")
    public Result<?> createPart(@RequestBody CreatePartVO createPart){
    	return this.zzShoporderService.createPart(createPart);
    }
    
    /**
     * @Description 分切mes拆单
     * @param splitMes
     * @return
     * Create at: 2021-01-22 16:45:49
     * @author: duany
     * Revision:
     *    2021-01-22 16:45:49 - first revision by duany
     *        
     */
    @PostMapping("/splitPartMes")
    public Result<?> splitPartMes(@RequestBody SplitMesVO splitMes){
    	return this.zzShoporderService.splitPartMes(splitMes);
    }

	/**
	 * 切张mes拆单
	 * @param splitMes
	 * @return
	 */
	@PostMapping("/splitPartMesQz")
    public Result<?> splitPartMesQz(@RequestBody SplitMesVO splitMes){
    	return this.zzShoporderService.splitPartMesQz(splitMes);
    }

	/**
	 * 复合mes拆单
	 * @param splitMes
	 * @return
	 */
	@PostMapping("/splitPartMesFH")
	public Result<?> splitPartMesFH(@RequestBody SplitMesVO splitMes){
		return this.zzShoporderService.splitPartMesFH(splitMes);
	}
    /**
	 * 切张任务单拆单
	 * @param splitMes
	 * @return
	 */
	@PostMapping("/splitTaskQz")
    public Result<?> splitTaskQz(@RequestBody SplitTaskVO splitMes){
    	return this.zzShoporderService.splitTask(splitMes);
    }


	@PostMapping("/directShip")
	public Result<?> directShip(@RequestBody CreatePickVO createPickVO){
		return zzShoporderService.directShip(createPickVO);
	}
}
